from dataclasses import dataclass
from datetime import datetime


from ctaf_core.ctaf_objects.base_data_objects import OrderEventData, QuoteEventData
from ctaf_facade.enumerations.exchange_enum import ExchangeEnum
from ctaf_facade.enumerations.trade_related_enum import DirectionEnum, OrderTypeEnum, OffsetEnum, IntervalEnum


@dataclass
class SubscribeRequest:
    """
    Request sending to specific gateway for subscribing tick data update.
    """

    symbol: str
    exchange: ExchangeEnum

    def __post_init__(self) -> None:
        """"""
        self.ctaf_symbol: str = f"{self.symbol}.{self.exchange.value}"


@dataclass
class OrderRequest:
    """
    Request sending to specific gateway for creating a new order.
    """

    symbol: str
    exchange: ExchangeEnum
    direction: DirectionEnum
    type: OrderTypeEnum
    volume: float
    price: float = 0
    offset: OffsetEnum = OffsetEnum.NONE
    reference: str = ""

    def __post_init__(self) -> None:
        """"""
        self.ctaf_symbol: str = f"{self.symbol}.{self.exchange.value}"

    def create_order_data(self, orderid: str, gateway_name: str) -> OrderEventData:
        """
        Create order data from request.
        """
        order: OrderEventData = OrderEventData(
            symbol=self.symbol,
            exchange=self.exchange,
            orderid=orderid,
            type=self.type,
            direction=self.direction,
            offset=self.offset,
            price=self.price,
            volume=self.volume,
            reference=self.reference,
            gateway_name=gateway_name,
        )
        return order


@dataclass
class CancelRequest:
    """
    Request sending to specific gateway for canceling an existing order.
    """

    orderid: str
    symbol: str
    exchange: ExchangeEnum

    def __post_init__(self) -> None:
        """"""
        self.ctaf_symbol: str = f"{self.symbol}.{self.exchange.value}"


@dataclass
class HistoryRequest:
    """
    Request sending to specific gateway for querying history data.
    """

    symbol: str
    exchange: ExchangeEnum
    start: datetime
    end: datetime = None
    interval: IntervalEnum = None

    def __post_init__(self) -> None:
        """"""
        self.ctaf_symbol: str = f"{self.symbol}.{self.exchange.value}"


@dataclass
class QuoteRequest:
    """
    Request sending to specific gateway for creating a new quote.
    """

    symbol: str
    exchange: ExchangeEnum
    bid_price: float
    bid_volume: int
    ask_price: float
    ask_volume: int
    bid_offset: OffsetEnum = OffsetEnum.NONE
    ask_offset: OffsetEnum = OffsetEnum.NONE
    reference: str = ""

    def __post_init__(self) -> None:
        """"""
        self.ctaf_symbol: str = f"{self.symbol}.{self.exchange.value}"

    def create_quote_data(self, quoteid: str, gateway_name: str) -> QuoteEventData:
        """
        Create quote data from request.
        """
        quote: QuoteEventData = QuoteEventData(
            symbol=self.symbol,
            exchange=self.exchange,
            quoteid=quoteid,
            bid_price=self.bid_price,
            bid_volume=self.bid_volume,
            ask_price=self.ask_price,
            ask_volume=self.ask_volume,
            bid_offset=self.bid_offset,
            ask_offset=self.ask_offset,
            reference=self.reference,
            gateway_name=gateway_name,
        )
        return quote